'use strict';
require('@/css/jk_minepage.css');
exports = module.exports = function($scope, $timeout, $state) {
    $scope.lllog = function(x){
        console.info(x)
    };
    $scope.mineModel = {
        ltListInit: [], lbListInit: [],
        clear: function(){
            this.ltList = this.ltListInit;
            this.lbList = this.lbListInit;
            this.rtList = [];
            this.rbList = [];
        },
        // 初始化 数据备份
        init: function(){
            $timeout(function(){
                var x1 = [
                    {id: 24,code:"003003",name:"优惠券",value:88},
                    {id: 22,code:"003001",name:"余额",value:88.88,subrouter:"app.ng.dndLists3.son3"},
                    {id: 23,code:"003002",name:"能量钱包",value:88},
                    {id: 26,code:"003005",name:"能量值",value:888},
                    {id: 25,code:"003004",name:"分享金",value:88.88}
                ];
                for( var i=0,l=x1.length;i<l;i++){ x1[i].index = i }
                var x2 = [
                    {id: 1,code:"001001",name:"个人资料"}, {id: 2,code:"001002",name:"会员权益说明"}, {id: 3,code:"001003",name:"会籍卡"},
                    {id: 4,code:"001004",name:"我的课程"}, {id: 5,code:"001005",name:"身体档案",subrouter:"app.ng.dndLists3.son2"}, {id: 6,code:"001006",name:"预约记录"},
                    {id: 7,code:"001007",name:"消息中心"}, {id: 8,code:"001008",name:"我的活动"}, {id: 17,code:"001009",name:"我的赠送"},
                    {id: 18,code:"001010",name:"帮助中心",subrouter:"app.ng.dndLists3.son1"}, {id: 20,code:"001011",name:"我要换肤"}, {id: 21,code:"001012",name:"训练计划"},
                    {id: 14,code:"002001",name:"活动中心"}, {id: 15,code:"002002",name:"推荐有礼"}, {id: 16,code:"002003",name:"我的福利"},
                    {id: 27,code:"002004",name:"我的售后"}, {id: 28,code:"002005",name:"我的秒杀"}, {id: 29,code:"002006",name:"我的订单"},
                    {id: 30,code:"002007",name:"我要推广"}, {id: 31,code:"002008",name:"积分商城"}, {id: 32,code:"002009",name:"我的拼团"},
                    {id: 33,code:"002010",name:"我的评价"}
                ];
                for( var i=0,l=x2.length;i<l;i++){ x2[i].index = i }
                this.ltListInit = x1;
                this.lbListInit = x2;
                this.clear();
                this.ajax();
            }.bind(this))
        },
        // 左上初始化
        ltinit: function( list ){
            for(var i=0,l=list.length;i<l;i++){
                this.ltList[list[i]].disabled = true
                this.rtList.push(angular.copy(this.ltList[list[i]]));
            }
        },
        // 左下边初始化
        lbinit: function( list ){
            for(var i=0,l=list.length;i<l;i++){
                this.lbList[list[i]].disabled = true
                this.rbList.push(angular.copy(this.lbList[list[i]]));
            }
        },
        // 左上边拷走 第一参数是自己调 第二参数是其他人调
        ltcopied: function( item, row ){
            console.info("左上",item.name,"到拖拽,左上标签被禁止")
            if ( item ) { item.disabled = true }
            else { this.ltList[row.index].disabled = true }
        },
        // 左下边拷走 第一参数是自己调 第二参数是其他人调
        lbcopied: function( item, row ){
            console.info("左下",item.name,"到拖拽,左下标签被禁止")
            if ( item ) { item.disabled = true }
            else { this.lbList[row.index].disabled = true }
        },
        // 右上 拖动
        // 拖动开始的时候就删掉元素 就不会触发moved方法了
        rtdrag: function( $index, row ){
            console.info("右上", row.name, "被移动");
        },
        // 右上 移除
        rtremoved: function( $index, row ){
            console.info("右上", row.name, "被删除", "左上标签放开");
            this.ltList[row.index].disabled = false;
            this.rtList.splice($index, 1);
        },
        // 右上 排序
        rtmoved: function( $index, row ){
            console.info("右上", row.name, "被排序", "左上标签不动");
            this.rtList.splice($index, 1);
        },
        // 右下 拖动
        rbdrag: function( $index, row ){
            console.info("右下", row.name, "被移动");
        },
        // 右下 移除
        rbremoved: function( $index, row ){
            console.info("右下", row.name, "被删除", "左下标签放开");
            this.lbList[row.index].disabled = false;
            this.rbList.splice($index, 1);
        },
        // 右下 排序
        rbmoved: function( $index, row ){
            console.info("右下", row.name, "被排序", "左下标签不动");
            this.rbList.splice($index, 1);
        },
        // 右 关闭逻辑
        rtclose: function( $index, row ){
            this.rtremoved( $index, row );
        },
        rbclose: function( $index, row ){
            this.rbremoved( $index, row );
        },
        subrouter: null,
        openPage: function( row ){
            console.info("子路由", row.name, "打开");
            if(row.subrouter){
                $state.go(row.subrouter);
                this.subrouter = row.code
            } else {
                this.subrouter = ""
            }
        },
        closePage: function( ){
            console.info("关闭子路由")
            $state.go("app.ng.dndLists3");
        },
        save: function(){
            var x = {list:[]}
            for(var i=0,l=this.rtList.length;i<l;i++){
                x.list.push({id:this.rtList[i].id})
            }
            for(var i=0,l=this.rbList.length;i<l;i++){
                x.list.push({id:this.rbList[i].id})
            }
            for(var i=0,l=x.list.length;i<l;i++){
                x.list[i].show_order = i+1
            }
            $scope.result = x
            console.info('提交表单', x)
        },
        recover: function(){
            console.info("恢复默认")
            this.clear();
            this.ltinit([0,1,2]);
            this.lbinit([0,2,3,5,8,9]);
        },
        ajax: function(){
            $timeout(function(){
                var x = [
                    {id: 22, name: "余额", code: "003001", show_group: 3},
                    {id: 23, name: "能量钱包", code: "003002", show_group: 3},
                    {id: 24, name: "优惠券", code: "003003", show_group: 3},
                    {id: 6, code: "001006", name: "预约记录", show_order: 1, show_group: 1},
                    {id: 17, code: "001009", name: "我的赠送", show_order: 2, show_group: 1},
                    {id: 18, code: "001010", name: "帮助中心", show_order: 3, show_group: 1},
                    {id: 7, code: "001007", name: "消息中心", show_order: 4, show_group: 1},
                    {id: 5, code: "001005", name: "身体档案", show_order: 5, show_group: 1},
                    {id: 3, code: "001003", name: "我的卡包", show_order: 6, show_group: 1},
                    {id: 2, code: "001002", name: "会员权益说明", show_order: 7, show_group: 1},
                    {id: 8, code: "001008", name: "我的活动", show_order: 8, show_group: 1},
                    {id: 21, code: "001012", name: "训练计划", show_order: 9, show_group: 1},
                    {id: 16, code: "002003", name: "我的福利", show_order: 10, show_group: 2},
                    {id: 4, code: "001004", name: "我的课程", show_order: 11, show_group: 1},
                    {id: 15, code: "002002", name: "推荐有礼", show_order: 12, show_group: 2},
                    {id: 14, code: "002001", name: "活动中心", show_order: 13, show_group: 2},
                    {id: 20, code: "001011", name: "我要换肤", show_order: 14, show_group: 1}
                ]
                //debugger;
                for(var i=0,l=x.length;i<l;i++){
                    var row = x[i]
                    if(row.show_group==3){
                        for(var ii=0,li=this.ltList.length;ii<li;ii++){
                            if(this.ltList[ii].code==row.code){
                                this.rtList.push(
                                    angular.copy(this.ltList[ii])
                                );
                                this.ltList[ii].disabled = true
                                break;
                            }
                        }
                    }else if(row.show_group==1||row.show_group==2||row.show_group==4){
                        for(var ii=0,li=this.lbList.length;ii<li;ii++){
                            if(this.lbList[ii].code==row.code){
                                this.rbList.push(
                                    angular.copy(this.lbList[ii])
                                );
                                this.lbList[ii].disabled = true
                                break;
                            }
                        }
                    }
                }
            }.bind(this))
        }
    };
    $scope.mineModel.init()
}